#!/bin/csh -f
#
# whnamchk: Script for running WhatIf to produce an atom map.
# A separate map is produced for each model.
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The script tries to find the location of the whatif directory. If this
# fails it must be hard coded (see below).

if ( $#argv == 0 ) then
    echo "usage: whnamchk  [-k]  pdb-file"
    exit (1)
endif

set keep = 0
if ( "$argv[1]" == "-k" ) then
    set keep = 1
    shift
endif

source $aquaroot/aqsetup

if ( ! -e $1 ) then
    echo "file $1 does not exist"
    exit (1)
endif

# make a scratch directory and go to it
mkdir wh.tmp
if ( $status ) then
    exit (1)
endif
cd wh.tmp

# split the input file, and make a list of the file(s) to process
set lead = `(echo $1 | cut -c1)`
if ( "$lead" == "/" ) then
    set fil = $1
else
    set fil = ../$1
endif
splitpdb $fil
set NOSPLIT = $status
if ( $NOSPLIT ) then
    cp $fil .
endif
set list = (`\ls *`)

# find the whatif directory, and make a link to TOPOLOGY.H
# if the find does not work, hard code the location of TOPOLOGY.H here
set fil = `which whatif`
set dir = $fil:h
ln -s $dir/dbdata/TOPOLOGY.H TOPOLOGY.FIL

# create the input file for whatif
echo SOUP               > STARTUP.FIL

# whatif parameters
echo "# Read one or all MODELs in NMR structures etc."  >> STARTUP.FIL
echo "#    =-1   Prompt the user what he/she wants (reading N models)" >> STARTUP.FIL
echo "#    = 0   Read all models" >> STARTUP.FIL
echo "#    = 1   Read only the first model" >> STARTUP.FIL
echo "SETWIF 847 1" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL
echo "#    = 0 Old PDB style 2HB in stead of HB2" >> STARTUP.FIL
echo "#    = 1 IUPAC nomenclature" >> STARTUP.FIL
echo "SETWIF 142 1" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL
echo "#    = 400 Cutoff for reporting deviations in the INP* routines (*100)" >> STARTUP.FIL
echo "SETWIF 143 400" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL
echo "# Default maximum number of entries in a PDBOUT table" >> STARTUP.FIL
echo "SETWIF 593 100" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL
echo "# Add C-terminal O if missing" >> STARTUP.FIL
echo "# 0=yes" >> STARTUP.FIL
echo "# 1=no" >> STARTUP.FIL
echo "SETWIF 1071 1" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL
echo "# Guess whether we have an NMR structure here."   >> STARTUP.FIL
echo "# 1=yes" >> STARTUP.FIL
echo "# 0=no" >> STARTUP.FIL
echo "SETWIF 1503 1" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL
echo "# Should "Q" atoms be considered hydrogen atoms?" >> STARTUP.FIL
echo "# =0: yes, as usual" >> STARTUP.FIL
echo "# =1: no" >> STARTUP.FIL
echo "SETWIF 1505 1" >> STARTUP.FIL
echo                                                    >> STARTUP.FIL

foreach x ( $list )
 echo DOLOG            >> STARTUP.FIL
 echo $x:r.lista       >> STARTUP.FIL
 echo 0                >> STARTUP.FIL
 echo INISOU           >> STARTUP.FIL
 echo GETMOL           >> STARTUP.FIL
 echo $x               >> STARTUP.FIL
 echo A                >> STARTUP.FIL
 echo LISTA            >> STARTUP.FIL
 echo ALL              >> STARTUP.FIL
 echo NOLOG            >> STARTUP.FIL
end
echo FULLSTOP          >> STARTUP.FIL
echo Y                 >> STARTUP.FIL

# run whatif
echo "starting whatif"
whatif >& wh.log

# postprocessing
foreach x ( $list )
 echo "processing $x:r.lista"
 wh2lib $x:r.lista   >   ../$x:r.lib
 if ( $keep ) \mv $x:r.lista ..
end

# clean up
cd ..
\rm -r wh.tmp
